Completed
Push — master ( ff1498...661f20 )
by Jeroen De
27s queued 13s
created

tab.js ➔ show   C

Complexity

Conditions 9

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 35
c 0
b 0
f 0
dl 0
loc 58
rs 6.6666

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*!
2
  * Bootstrap tab.js v4.6.0 (https://getbootstrap.com/)
3
  * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
  */
6 View Code Duplication
(function (global, factory) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
8
  typeof define === 'function' && define.amd ? define(['jquery', './util'], factory) :
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Tab = factory(global.jQuery, global.Util));
0 ignored issues
show
Bug introduced by
The variable globalThis seems to be never declared. If this is a global, consider adding a /** global: globalThis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Best Practice introduced by
If you intend to check if the variable self is declared in the current environment, consider using typeof self === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
10
}(this, (function ($, Util) { 'use strict';
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
  var $__default = /*#__PURE__*/_interopDefaultLegacy($);
15
  var Util__default = /*#__PURE__*/_interopDefaultLegacy(Util);
16
17
  function _defineProperties(target, props) {
18
    for (var i = 0; i < props.length; i++) {
19
      var descriptor = props[i];
20
      descriptor.enumerable = descriptor.enumerable || false;
21
      descriptor.configurable = true;
22
      if ("value" in descriptor) descriptor.writable = true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
23
      Object.defineProperty(target, descriptor.key, descriptor);
24
    }
25
  }
26
27
  function _createClass(Constructor, protoProps, staticProps) {
28
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
    if (staticProps) _defineProperties(Constructor, staticProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
30
    return Constructor;
31
  }
32
33
  /**
34
   * ------------------------------------------------------------------------
35
   * Constants
36
   * ------------------------------------------------------------------------
37
   */
38
39
  var NAME = 'tab';
40
  var VERSION = '4.6.0';
41
  var DATA_KEY = 'bs.tab';
42
  var EVENT_KEY = "." + DATA_KEY;
43
  var DATA_API_KEY = '.data-api';
44
  var JQUERY_NO_CONFLICT = $__default['default'].fn[NAME];
45
  var EVENT_HIDE = "hide" + EVENT_KEY;
46
  var EVENT_HIDDEN = "hidden" + EVENT_KEY;
47
  var EVENT_SHOW = "show" + EVENT_KEY;
48
  var EVENT_SHOWN = "shown" + EVENT_KEY;
49
  var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
50
  var CLASS_NAME_DROPDOWN_MENU = 'dropdown-menu';
51
  var CLASS_NAME_ACTIVE = 'active';
52
  var CLASS_NAME_DISABLED = 'disabled';
53
  var CLASS_NAME_FADE = 'fade';
54
  var CLASS_NAME_SHOW = 'show';
55
  var SELECTOR_DROPDOWN = '.dropdown';
56
  var SELECTOR_NAV_LIST_GROUP = '.nav, .list-group';
57
  var SELECTOR_ACTIVE = '.active';
58
  var SELECTOR_ACTIVE_UL = '> li > .active';
59
  var SELECTOR_DATA_TOGGLE = '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]';
60
  var SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle';
61
  var SELECTOR_DROPDOWN_ACTIVE_CHILD = '> .dropdown-menu .active';
62
  /**
63
   * ------------------------------------------------------------------------
64
   * Class Definition
65
   * ------------------------------------------------------------------------
66
   */
67
68
  var Tab = /*#__PURE__*/function () {
69
    function Tab(element) {
70
      this._element = element;
71
    } // Getters
72
73
74
    var _proto = Tab.prototype;
75
76
    // Public
77
    _proto.show = function show() {
78
      var _this = this;
79
80
      if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && $__default['default'](this._element).hasClass(CLASS_NAME_ACTIVE) || $__default['default'](this._element).hasClass(CLASS_NAME_DISABLED)) {
0 ignored issues
show
Bug introduced by
The variable Node seems to be never declared. If this is a global, consider adding a /** global: Node */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
81
        return;
82
      }
83
84
      var target;
85
      var previous;
86
      var listElement = $__default['default'](this._element).closest(SELECTOR_NAV_LIST_GROUP)[0];
87
      var selector = Util__default['default'].getSelectorFromElement(this._element);
88
89
      if (listElement) {
90
        var itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? SELECTOR_ACTIVE_UL : SELECTOR_ACTIVE;
91
        previous = $__default['default'].makeArray($__default['default'](listElement).find(itemSelector));
92
        previous = previous[previous.length - 1];
93
      }
94
95
      var hideEvent = $__default['default'].Event(EVENT_HIDE, {
96
        relatedTarget: this._element
97
      });
98
      var showEvent = $__default['default'].Event(EVENT_SHOW, {
99
        relatedTarget: previous
0 ignored issues
show
Bug introduced by
The variable previous does not seem to be initialized in case listElement on line 89 is false. Are you sure this can never be the case?
Loading history...
100
      });
101
102
      if (previous) {
103
        $__default['default'](previous).trigger(hideEvent);
104
      }
105
106
      $__default['default'](this._element).trigger(showEvent);
107
108
      if (showEvent.isDefaultPrevented() || hideEvent.isDefaultPrevented()) {
109
        return;
110
      }
111
112
      if (selector) {
113
        target = document.querySelector(selector);
114
      }
115
116
      this._activate(this._element, listElement);
117
118
      var complete = function complete() {
119
        var hiddenEvent = $__default['default'].Event(EVENT_HIDDEN, {
120
          relatedTarget: _this._element
121
        });
122
        var shownEvent = $__default['default'].Event(EVENT_SHOWN, {
123
          relatedTarget: previous
0 ignored issues
show
Bug introduced by
The variable previous does not seem to be initialized in case listElement on line 89 is false. Are you sure this can never be the case?
Loading history...
124
        });
125
        $__default['default'](previous).trigger(hiddenEvent);
126
        $__default['default'](_this._element).trigger(shownEvent);
127
      };
128
129
      if (target) {
130
        this._activate(target, target.parentNode, complete);
131
      } else {
132
        complete();
133
      }
134
    };
135
136
    _proto.dispose = function dispose() {
137
      $__default['default'].removeData(this._element, DATA_KEY);
138
      this._element = null;
139
    } // Private
140
    ;
141
142
    _proto._activate = function _activate(element, container, callback) {
143
      var _this2 = this;
144
145
      var activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ? $__default['default'](container).find(SELECTOR_ACTIVE_UL) : $__default['default'](container).children(SELECTOR_ACTIVE);
146
      var active = activeElements[0];
147
      var isTransitioning = callback && active && $__default['default'](active).hasClass(CLASS_NAME_FADE);
148
149
      var complete = function complete() {
150
        return _this2._transitionComplete(element, active, callback);
151
      };
152
153
      if (active && isTransitioning) {
154
        var transitionDuration = Util__default['default'].getTransitionDurationFromElement(active);
155
        $__default['default'](active).removeClass(CLASS_NAME_SHOW).one(Util__default['default'].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
156
      } else {
157
        complete();
158
      }
159
    };
160
161
    _proto._transitionComplete = function _transitionComplete(element, active, callback) {
162
      if (active) {
163
        $__default['default'](active).removeClass(CLASS_NAME_ACTIVE);
164
        var dropdownChild = $__default['default'](active.parentNode).find(SELECTOR_DROPDOWN_ACTIVE_CHILD)[0];
165
166
        if (dropdownChild) {
167
          $__default['default'](dropdownChild).removeClass(CLASS_NAME_ACTIVE);
168
        }
169
170
        if (active.getAttribute('role') === 'tab') {
171
          active.setAttribute('aria-selected', false);
172
        }
173
      }
174
175
      $__default['default'](element).addClass(CLASS_NAME_ACTIVE);
176
177
      if (element.getAttribute('role') === 'tab') {
178
        element.setAttribute('aria-selected', true);
179
      }
180
181
      Util__default['default'].reflow(element);
182
183
      if (element.classList.contains(CLASS_NAME_FADE)) {
184
        element.classList.add(CLASS_NAME_SHOW);
185
      }
186
187
      if (element.parentNode && $__default['default'](element.parentNode).hasClass(CLASS_NAME_DROPDOWN_MENU)) {
188
        var dropdownElement = $__default['default'](element).closest(SELECTOR_DROPDOWN)[0];
189
190
        if (dropdownElement) {
191
          var dropdownToggleList = [].slice.call(dropdownElement.querySelectorAll(SELECTOR_DROPDOWN_TOGGLE));
192
          $__default['default'](dropdownToggleList).addClass(CLASS_NAME_ACTIVE);
193
        }
194
195
        element.setAttribute('aria-expanded', true);
196
      }
197
198
      if (callback) {
199
        callback();
200
      }
201
    } // Static
202
    ;
203
204
    Tab._jQueryInterface = function _jQueryInterface(config) {
205
      return this.each(function () {
206
        var $this = $__default['default'](this);
207
        var data = $this.data(DATA_KEY);
208
209
        if (!data) {
210
          data = new Tab(this);
211
          $this.data(DATA_KEY, data);
212
        }
213
214
        if (typeof config === 'string') {
215
          if (typeof data[config] === 'undefined') {
216
            throw new TypeError("No method named \"" + config + "\"");
217
          }
218
219
          data[config]();
220
        }
221
      });
222
    };
223
224
    _createClass(Tab, null, [{
225
      key: "VERSION",
226
      get: function get() {
227
        return VERSION;
228
      }
229
    }]);
230
231
    return Tab;
232
  }();
233
  /**
234
   * ------------------------------------------------------------------------
235
   * Data Api implementation
236
   * ------------------------------------------------------------------------
237
   */
238
239
240
  $__default['default'](document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
241
    event.preventDefault();
242
243
    Tab._jQueryInterface.call($__default['default'](this), 'show');
244
  });
245
  /**
246
   * ------------------------------------------------------------------------
247
   * jQuery
248
   * ------------------------------------------------------------------------
249
   */
250
251
  $__default['default'].fn[NAME] = Tab._jQueryInterface;
252
  $__default['default'].fn[NAME].Constructor = Tab;
253
254
  $__default['default'].fn[NAME].noConflict = function () {
255
    $__default['default'].fn[NAME] = JQUERY_NO_CONFLICT;
256
    return Tab._jQueryInterface;
257
  };
258
259
  return Tab;
260
261
})));
262
//# sourceMappingURL=tab.js.map
263